﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Observer_Pattern
{
    class StockGrabber : Subject 
    {
        private List<IObserver> observers;
        private double ibmPrice;
        private double aaplPrice;
        private double googPrice;

        public StockGrabber()
        {
            this.observers = new List<IObserver>();
        }

        public override void attach(IObserver newObserver)
        {
            observers.Add(newObserver);
        }

        public override void dettach(IObserver deleteObserver)
        {
            int observerIndex = observers.IndexOf(deleteObserver);
            observers.RemoveAt(observerIndex);
        }

        public override void notify()
        {
            foreach (IObserver observer in observers)
            {
                observer.update(ibmPrice, aaplPrice, googPrice);
            }
        }

        public void setIBMPrice(double newPrice)
        {
            this.ibmPrice = newPrice;
            notify();
        }

        public void setAAPLPrice(double newPrice)
        {
            this.aaplPrice = newPrice;
            notify();
        }

        public void setGoogPrice(double newPrice)
        {
            this.googPrice = newPrice;
            notify();
        }
    }
}
